package com.ssm.comm.util;

import org.springframework.boot.web.servlet.server.Session;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Objects;

public class HttpHandlerUtil {
    private HttpHandlerUtil(){

    }

    public static HttpSession getSession(){
        HttpServletRequest req =  getRequest();
        if(Objects.isNull(req)){
            return null ;
        }
        return req.getSession() ;
    }

    public static HttpServletRequest getRequest(){
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.
                getRequestAttributes();
        if(null == sra){
			//.....
            return null;
        }
        return sra.getRequest();
    }

    public static HttpServletResponse getResponse(){
        ServletRequestAttributes sra = (ServletRequestAttributes)RequestContextHolder.
                getRequestAttributes();
        if(null == sra){
			//.....
            return null;
        }
        return sra.getResponse();
    }

    public static String getDomain(){
        HttpServletRequest request = getRequest();
        StringBuffer url = request.getRequestURL();
        return url.delete(url.length() - request.getRequestURI().length(), url.length()).toString();
    }

    public static String getOrigin(){
        HttpServletRequest request = getRequest();
        return request.getHeader("Origin");
    }
}
